/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.drone.ui.instrument.dial;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import fr.uha.ensisa.jmp.drone.ui.instrument.dial.model.DefaultDialModel;
import fr.uha.ensisa.jmp.drone.ui.instrument.dial.model.DialModel;
import fr.uha.ensisa.jmp.drone.ui.instrument.dial.renderer.DefaultDialRenderer;
import fr.uha.ensisa.jmp.drone.ui.instrument.dial.renderer.DialRenderer;


public class Dial extends JComponent
{
	private DialModel dialModel;

	private ChangeListener changeListener;
	private ChangeEvent changeEvent = null;

	private DialRenderer renderer;

	public Dial(DialModel model)
	{
		this.dialModel = model;
		this.changeListener = new ModelListener();
		this.setModel(model);
		this.renderer = new DefaultDialRenderer(this);
	}

	public Dial()
	{
		this(new DefaultDialModel());
	}

	public DialModel getModel()
	{
		return this.dialModel;
	}

	public DialRenderer getRenderer()
	{
		return this.renderer;
	}

	public void setRenderer(DialRenderer renderer)
	{
		this.renderer = renderer;
	}

	public void setModel(DialModel newModel)
	{
		DialModel oldModel = this.dialModel;

		if (oldModel != null)
		{
			oldModel.removeChangeListener(this.changeListener);
		}

		this.dialModel = newModel;

		if (newModel != null)
		{
			newModel.addChangeListener(this.changeListener);

		}
	}

	public void addChangeListener(ChangeListener l)
	{
		listenerList.add(ChangeListener.class, l);
	}

	public void removeChangeListener(ChangeListener l)
	{
		listenerList.remove(ChangeListener.class, l);
	}

	public ChangeListener[] getChangeListeners()
	{
		return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
	}
	
	protected void fireStateChanged()
	{
		Object[] listeners = listenerList.getListenerList();
		for (int i = listeners.length - 2; i >= 0; i -= 2)
		{
			if (listeners[i] == ChangeListener.class)
			{
				if (changeEvent == null)
				{
					changeEvent = new ChangeEvent(this);
				}
				((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);

			}
		}
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponents(g);
		this.renderer.renderDial((Graphics2D) g);
	}
	
	@Override
	public Dimension getMaximumSize()
	{
		return this.renderer.getMaximumSize();
	}

	@Override
	public Dimension getMinimumSize()
	{		
		return this.renderer.getMinimumSize();
	}

	@Override
	public Dimension getPreferredSize()
	{
		return this.renderer.getPreferredSize();
	}

	//class ModelListener---------------------------
	private class ModelListener implements ChangeListener
	{
		public void stateChanged(ChangeEvent e)
		{
			fireStateChanged();
		}
	}
}
